
var express = require('express');
var bodyParser = require('body-parser')
var whiteList = require('./accessfile/accessDomin.js')
var cors = require('cors');
var multiparty = require('multiparty');
var fse = require('fs-extra');
var app = express();
// 解决跨域
app.use(cors())

// 接口白名单
console.log(whiteList);

// 静态网页主目录
app.use(express.static(__dirname + '/public'));

// 解析参数，中间件
app.use(bodyParser.urlencoded({
	extended: true
}));

app.use(bodyParser.json());

// 上传接口
app.post('/upload', function(req, res) {

	res.header("x-powered-by", "wocao")

	console.log("白名单：", whiteList.includes(req.headers.origin));

	// 判断是否为白名单
	
	if (whiteList.includes(req.headers.origin) == true) {
		res.header("Access-Control-Allow-Origin", req.headers.origin);
		res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
		console.log(req.headers.origin);
		
		// 指定上传文件存储目录
		const form = new multiparty.Form({
			uploadDir: 'temp',
		});
		// console.log(form);
		// 解析参数
		form.parse(req)
		
		form.on('file', function() {

		})
		
		res.send('上传成功')
		
	} else {

	// 如果没有在白名单中接口无法使用
		res.status(403).send({error:'无权限，需要开启相关接口访问'});

		console.log();
	}








})


app.listen(3000);
console.log('127.0.0.1:3000')
